Ext.namespace('Ext.ux.calendar');

Ext.ux.calendar.SelectUserGrid = Ext.extend(Ext.grid.GridPanel,{
	heigth: 200,
	initComponent: function(){
		
		this.sm = new Ext.grid.CheckboxSelectionModel();
		var fields = [
			{name: 'shareUserId', mapping: 'shareUserId'},
			{name: 'shareUserName', mapping: 'shareUserName'},
			{name: 'shareType', mapping: 'shareType'},
			{name: 'shareTypeName', mapping: 'shareTypeName'}
		]
		
		var columns = [
			this.sm,
			{dataIndex: 'shareUserId', hidden: true},
			{dataIndex: 'shareType',hidden: true},
			{header: '用户名称', width: 100, dataIndex: 'shareUserName'},
			{header: '共享类型', width: 100, dataIndex: 'shareTypeName'},
			{header: '操作', width: 100, dataIndex: ''}
			
		];
		
		this.columns = columns;
		
		this.ds = new Ext.data.JsonStore({
			proxy: new Ext.data.HttpProxy({
				url: ''
			}),
	        fields: fields,
			root: 'result'
	    });
		this.store = this.ds;
		
		
		this.tbar = new Ext.Toolbar();
		this.tbar.addItem('-');
		this.tbar.addItem({
			text: '修改日程',
			tooltip: '新增',
			iconCls: 'icon-add',
			handler: this.onAddEditor,

			scope: this
		});
		this.tbar.addItem('-');
		this.tbar.addItem({
			text: '公开日程',
			tooltip: '新增',
			iconCls: 'icon-add',
			handler: this.onAddReader,
			scope: this
		});
		this.tbar.addItem('-');
		this.tbar.addItem({
			text:'删除',
			tooltip: '删除共享者',
			iconCls: 'icon-delete',
			handler: this.deleteShareUser,
			scope: this
		});
		
		this.tbar.addItem('-');
		this.tbar.addItem({
			text: '清空',
			tooltip: '全部清空',
			iconCls: 'icon-clearall',
			handler: function(){
				this.store.removeAll();
			},
			scope: this
		});
		
		
		Ext.ux.calendar.SelectUserGrid.superclass.initComponent.call(this);
		
	},
	onAddEditor: function(){
		var selectUserWin = new Ext.ux.calendar.SelectUserWindow({
				parentPanel:this,
				url: ctx + '/calendar/sys-calendar!getOrgChree.action',
				shareType: '可编辑'
		});
		
		selectUserWin.on('selectUsers', function(nodes){
			if(nodes != null && nodes.length > 0){
				for(var i = 0; i < nodes.length; i++){
					if(!this.isUserIdExist(nodes[i].attributes.keyWord))
						this.addRecord(nodes[i], selectUserWin.shareType);
				}
			}
		},this);
		selectUserWin.show();
	},
	onAddReader: function(){
		var selectUserWin = new Ext.ux.calendar.SelectUserWindow({
				parentPanel:this,
				url: ctx + '/calendar/sys-calendar!getOrgChree.action',
				shareType: '只读'
		});
		
		selectUserWin.on('selectUsers', function(nodes){
			if(nodes != null && nodes.length > 0){
				for(var i = 0; i < nodes.length; i++){
					if(!this.isUserIdExist(nodes[i].attributes.keyWord))
						this.addRecord(nodes[i], selectUserWin.shareType);
				}
			}
		},this);
		selectUserWin.show();
	},
	deleteShareUser: function(){
		var sels = this.getSelectionModel().getSelections();
		if(sels.length > 0){
			for(var i = 0; i < sels.length; i++){
				this.store.remove(sels[i]);
			}
		}
		else{
			
		}
	},
	addRecord: function(node, shareTypeName){
		var userId = node.attributes.keyWord;
		var userName = node.text;
		var orgName = node.parentNode.attributes.text;
		var shareType = shareTypeName == '只读'?'R':'W';
		 			
		var config = {
			shareUserId: userId,
			shareUserName: userName,
			orgName: orgName,
			shareType: shareType,
			shareTypeName: shareTypeName
			
		};
		var blankRecord =  Ext.data.Record.create(['shareUserId', 'shareUserName', 'orgName', 'shareType']);
		var record = new blankRecord(config);
		this.store.add(record);
	},
	isUserIdExist: function(userId){
		for(var i = 0; i < this.store.getCount(); i++){
			if(userId == this.store.getAt(i).get('shareUserId'))
				return true;
		}
		return false;
	},
	getShareUserIds: function(){
		var userIds = new Array();
		for(var i = 0; i < this.store.getCount(); i++){
			userIds.push(this.store.getAt(i).get('shareUserId'));
		}
		return userIds;
	},
	getShareTypes: function(){
		var userIds = new Array();
		for(var i = 0; i < this.store.getCount(); i++){
			userIds.push(this.store.getAt(i).get('shareType'));
		}
		return userIds;
	},
	refresh: function(eventId){
		this.store.removeAll();
		var url = ctx + '/calendar/sys-calendar!getShareUsers.action?eventId=' + eventId;
		this.store.proxy.setUrl(url, true);
		this.store.reload();
	}
});